const fs = require('fs');
const path = require('path');
const solc = require('solc');


const contractPath = path.resolve(__dirname, '../constract/Imooc.sol');

//获取文件内容
const source = fs.readFileSync(contractPath, 'utf-8');


// console.log(source);

//编译
const ret = solc.compile(source);

//如果出错
if (Array.isArray(ret.errors) && ret.errors.length > 0) {
    console.log(ret.errors[0]);
} else {
// console.log(ret);

    Object.keys(ret.contracts).forEach(name => {
        //slice(1) 去掉合约名称前面的冒号
        const contractsName = name.slice(1);
        const filePath = path.resolve(__dirname, `../src/compiled/${contractsName}.json`);
        fs.writeFileSync(filePath, JSON.stringify(ret.contracts[name]));
        console.log(`${filePath}`, 'succ');
    });

}
